package cn.zmf.springbootyuan.mapper;

import cn.zmf.springbootyuan.pojo.Department;
import cn.zmf.springbootyuan.pojo.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
@Repository
public class EmployeeMapper {
    //虚拟数据库数据
    private static Map<Integer, Employee> employees = null;
    @Autowired
    private DepartmentMapper departmentMapper;
    static {
        employees = new HashMap<>();
        employees.put(1001,new Employee(1001,"张梦凡","ayue413@163.com",new Department(101,"教学部"),1));
        employees.put(1002,new Employee(1002,"管延大","guan@163.com",new Department(102,"研发部"),0));
        employees.put(1003,new Employee(1003,"李思冶","siye@163.com",new Department(103,"事业部"),1));
        employees.put(1004,new Employee(1004,"刘洋","yang@163.com",new Department(104,"项目部"),0));
        employees.put(1005,new Employee(1005,"惨瑞","dog@163.com",new Department(105,"招生办"),1));

    }
    //主键自增
    private static Integer initId = 1006;
    //获取所有员工信息
    public Collection<Employee> getAll(){
        return employees.values();
    }

    //通过id查询学生
    public Employee getEmployeeById(Integer id) {
        return employees.get(id);
    }

    //删除员工通过id
    public void delete(Integer id) {
        employees.remove(id);
    }

    //增加一个员工
    private void save(Employee employee) {
        if (employee.getId() == null) {
            employee.setId(initId++);
        }
        employee.setDepartment(departmentMapper.getDepartmentById(employee.getDepartment().getId()));

        employees.put(employee.getId(),employee);
    }

}
